﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBHelp.DAL;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace OrderingMeals.MyVVM
{
    /// <summary>
    /// 登录
    /// </summary>
    public class LoginPageModel : ObservableObject, IQueryAttributable
    {
        /// <summary>
        /// 用户名
        /// </summary>
        private string? _username;
        /// <summary>
        /// 用户名
        /// </summary>
        public string? username
        {
            get => _username; set
            {
                _username = value;
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 密码
        /// </summary>
        private string? _passwork { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string? password
        {
            get => _passwork; set
            {
                _passwork = value;
                OnPropertyChanged();
            }
        }
        private string _erro;
        public string erro { get => _erro; set { _erro = value; OnPropertyChanged(); } }
        public ICommand LoadCommand {  get; set; }
        private UserDal userDal { get; set; }
        public LoginPageModel()
        {
            userDal = new UserDal();
            LoadCommand = new AsyncRelayCommand(Load);
            username = "admin";
            password = "admin";
        }

        private async Task Load()
        {
            erro = "";
            var data = await userDal.SelectData(username);
            if (data != null && data.Passward == password)
            {
                await Shell.Current.GoToAsync("mp");
                return;
            }
            else
            {
                erro = "用户查找错误或密码错误";
            }
        }

        public void ApplyQueryAttributes(IDictionary<string, object> query)
        {
            RefreshProperties();
        }
        private void RefreshProperties()
        {
            OnPropertyChanged(nameof(username));
            OnPropertyChanged(nameof(password));
        }
    }
}
